using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using YayiMgr.Ability.Docking.DictionaryApp;
using YayiMgr.Ability.Docking.DictionaryApp.Dto;
using YayiMgr.Domain.SystemBase;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace YayiMgr.Ability.DictionaryApp
{
    // public class DictionaryItemServicer: AutoAbilityServicer<>, IDictionaryItemServicer
    public class DictionaryItemServicer: AbilityServicer, IDictionaryItemServicer
    {
        public IRepository<DictionaryItem> DicItemRepo { get; set; }
        public async Task<List<DictionaryItemDto>> GetAllDictionaryItems()
        {
            List<DictionaryItem> items = await DicItemRepo.OrderBy(o=>o.TypeId).ToListAsync();//.GetListAsync();
            var itemDots = ModelMapper.Map<List<DictionaryItem>, List<DictionaryItemDto>>(items);
            return itemDots;
        }
        // public async Task<List<DictionaryItemDto>> GetAllDictionaryItemsByTypeId(Guid TypeId)
        // {
        //     List<DictionaryItem> items = await DicItemRepo.GetListAsync(i=>i.TypeId==TypeId);
        //     var itemDots = ModelMapper.Map<List<DictionaryItem>, List<DictionaryItemDto>>(items);
        //     return itemDots;
        // }

        public async Task<DictionaryItemDto> GetAllDictionaryItemsById(Guid Id)
        {
            DictionaryItem item = await DicItemRepo.GetAsync(Id);
            return ModelMapper.Map<DictionaryItem, DictionaryItemDto>(item);
        }

        public async Task<DictionaryItemDto> AddDictionaryItem(DictionaryItemCreateDto dto)
        {
            var item = ModelMapper.Map<DictionaryItemCreateDto, DictionaryItem>(dto);
            item.Id = SortGuid.NewGuid();
            item = await DicItemRepo.InsertAsync(item);
            var itemDto = ModelMapper.Map<DictionaryItem, DictionaryItemDto>(item);
            return itemDto;
        }

        public async Task<DictionaryItemDto> UpdateDictionaryItem(Guid Id, DictionaryItemUpdateDto dto)
        {
            DictionaryItem item = await DicItemRepo.GetAsync(Id);
            item = ModelMapper.Map<DictionaryItemUpdateDto, DictionaryItem>(dto);
            item.Id = Id;
            // if(item!=null)
            // {
            //     item.Name = dto.Name;
            //     item.TypeId = dto.TypeId;
            //     item.TypeName = dto.TypeName;
            //     item.Sort = dto.Sort;
            //     item.Enabled = dto.Enabled;
            // }
            item = await DicItemRepo.UpdateAsync(item);
            DictionaryItemDto dto1 = ModelMapper.Map<DictionaryItem,DictionaryItemDto>(item);
            return dto1;
        }

        public async Task DeleteDictionaryItem(Guid Id)
        {
            await DicItemRepo.DeleteAsync(Id);
        }
    }
}